﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.IO;

namespace HRM
{
    public partial class frmIncreaseSalaryList : DockContent
    {
        int pageSize = UIParamater.pageSize;
        int currPage = 0;
        int itemCount = 0;
        int totalPage = 0;
        int department_ID = 0;
        int branch_ID = 0;
        MainServiceClient wcfMain;
        EmployeeClient wcfEmployee;
        RoleRightInfo listRole;

        public frmIncreaseSalaryList()
        {
            InitializeComponent();
            //LoadLanguage();
            wcfEmployee = Service.Employee();
            wcfMain = Service.MainService();

            listRole = UIParamater.Quyen_CanhBaoNangLuong;
            if (!listRole.Update)
            {
                this.btnViewEmloyee.Image = global::HRM.Properties.Resources.app;
                this.btnViewEmloyee.Tag = "xem";
            }
            BuildVoucherGrid();
            UISetting.LoadTreeView3(ref tvBranch);
            //LoadData();
            this.KeyPreview = true;
            //
            txtToMonth.Text = DateTime.Now.ToString("MM/yyyy");
        }
        void LoadLanguage()
        {
            if (this.Tag != null)
                this.TabText = this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }
        private void BuildVoucherGrid()
        {
            UISetting.AddColumn(ref dgvList, "RowNumber", "STT", "", 50, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvList, "Employee_Code", UpdateUI.GetString("maso"), "", 100, true, true);
            UISetting.AddColumn(ref dgvList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 150, true, true);
            UISetting.AddColumn(ref dgvList, "Position_Name", "Chức vụ", "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "Grade_Name", "Ngạch-Chức danh", "", 120, true, true);
            UISetting.AddColumn(ref dgvList, "Level_Name", "Bậc", "", 60, true, true);
            UISetting.AddColumn(ref dgvList, "PD_BasicSalary", "Mức lương", "R", 120, true, true);
            UISetting.AddColumn(ref dgvList, "PD_BasicFactor", "HSL", "R", 150, true, true);
            UISetting.AddColumn(ref dgvList, "PD_BeginDate", "Tháng bắt đầu", "R", 150, true, true);
            UISetting.AddColumn(ref dgvList, "PD_DateSalary", "Tháng đến hạn", "R", 150, true, true);
            //UISetting.AddColumn(ref dgvList, "BRANCH_NAME", UpdateUI.GetString("chinhanh"), "", 200, true, true);
            UISetting.AddColumn(ref dgvList, "DEPARTMENT_NAME", UpdateUI.GetString("phongban"), "", 200, true, true);
        }
        private void LoadData()
        {
            
            DateTextBoxShort dtbFromDate = new DateTextBoxShort(txtToMonth);
            int _dayVal = 0;
            
            //if (cboMonth.Text.Length > 0)
            //    _dayVal = Convert.ToInt32(cboMonth.Text);

            DateTime _toMonth = UISetting.GetDate(txtToMonth.Text);
            DataSet _ds = wcfEmployee.Employee_IncreaseSalary(out itemCount,_dayVal, branch_ID, department_ID, _toMonth, currPage, pageSize, 1);
            PageIndex();
            dgvList.Rows.Clear();

            for (int i = 0; i < _ds.Tables[0].Rows.Count; i++)
            {
                dgvList.Rows.Insert(i, 1);
                dgvList.Rows[i].Cells["RowNumber"].Value = _ds.Tables[0].Rows[i]["RowNumber"].ToString();
                dgvList.Rows[i].Cells["Employee_ID"].Value = _ds.Tables[0].Rows[i]["Employee_ID"].ToString();
                dgvList.Rows[i].Cells["Employee_Code"].Value = _ds.Tables[0].Rows[i]["Employee_Code"].ToString();

                if (_ds.Tables[0].Columns.Contains("Employee_FullName"))
                    dgvList.Rows[i].Cells["Employee_FullName"].Value = _ds.Tables[0].Rows[i]["Employee_FullName"].ToString();
                else
                    dgvList.Rows[i].Cells["Employee_FullName"].Value = Convert.ToString(_ds.Tables[0].Rows[i]["Employee_LastName"]) + " " + Convert.ToString(_ds.Tables[0].Rows[i]["Employee_FirstName"]);

                dgvList.Rows[i].Cells["Position_Name"].Value = _ds.Tables[0].Rows[i]["Position_Name"].ToString();
                dgvList.Rows[i].Cells["Grade_Name"].Value = _ds.Tables[0].Rows[i]["Grade_Name"].ToString();
                dgvList.Rows[i].Cells["Level_Name"].Value = _ds.Tables[0].Rows[i]["Level_Name"].ToString();
                dgvList.Rows[i].Cells["PD_BasicSalary"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["PD_BasicSalary"]).ToString("N1");
                dgvList.Rows[i].Cells["PD_BasicFactor"].Value = Convert.ToDecimal(_ds.Tables[0].Rows[i]["PD_BasicFactor"]).ToString("N1");
                dgvList.Rows[i].Cells["PD_BeginDate"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["PD_BeginDate"]).ToString("MM/yyyy");
                dgvList.Rows[i].Cells["PD_DateSalary"].Value = Convert.ToDateTime(_ds.Tables[0].Rows[i]["PD_DateSalary"]).ToString("MM/yyyy");
                //dgvList.Rows[i].Cells["BRANCH_NAME"].Value = _ds.Tables[0].Rows[i]["BRANCH_NAME"].ToString();
                dgvList.Rows[i].Cells["DEPARTMENT_NAME"].Value = _ds.Tables[0].Rows[i]["DEPARTMENT_NAME"].ToString();

            }
        }
        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }


        private void btnPrint_Click(object sender, EventArgs e)
        {
            int _dayVal = 0;
            
            //if (cboMonth.Text.Length > 0)
            //    _dayVal = Convert.ToInt32(cboMonth.Text);

            DateTime _toMonth = UISetting.GetDate(txtToMonth.Text);
            //frmPreviewReport f = new frmPreviewReport("DSCB_SapLenLuong", new string[] { _month, _todate, branch_ID.ToString(), department_ID.ToString() });
            //f.Show(this);
            string _fileName = "";
            string branchName = "";
            string departmentName = "";
            BranchClient wcfBranch = Service.Branch();
            DepartmentClient wcfDepartment = Service.Department();
            _fileName = "tmpDSNangLuong" + DateTime.Now.ToString("HH:mm").Replace(":","_") + ".xls";
            if (branch_ID > 0)
            {
                branchName = wcfBranch.Branch_GetInfo(branch_ID).Name;
            }

            if (department_ID > 0)
            {
                departmentName = wcfDepartment.Department_GetInfo(department_ID).Name;
            }
            try
            {
                DataSet ds = wcfEmployee.Employee_IncreaseSalary(out itemCount, _dayVal, branch_ID, department_ID, _toMonth, currPage, pageSize, 1);
                if (ds.Tables.Count > 0)
                {
                    ds.Tables[0].Columns.Remove("Employee_ID");
                    ds.Tables[0].Columns.Remove("DEPARTMENT_ID");
                    ds.Tables[0].Columns.Remove("BRANCH_ID");
                    ds.Tables[0].Columns.Remove("BRANCH_NAME");
                    ds.Tables[0].Columns["Rownumber"].Caption = "STT";
                    ds.Tables[0].Columns["Employee_Code"].Caption = "Mã nhân viên";
                    ds.Tables[0].Columns["Employee_FullName"].Caption = "Họ và tên";
                    ds.Tables[0].Columns["Employee_DateofBirth"].Caption = "Ngày sinh";
                    ds.Tables[0].Columns["Employee_Sex"].Caption = "Giới tính";
                    ds.Tables[0].Columns["Department_Name"].Caption = "Phòng ban";
                    ds.Tables[0].Columns["Position_Name"].Caption = "Chức vụ";
                    ds.Tables[0].Columns["Grade_Name"].Caption = "Ngạch-Chức danh";
                    ds.Tables[0].Columns["Level_Name"].Caption = "Bậc";
                    ds.Tables[0].Columns["PD_BasicSalary"].Caption = "ML hiện tại";
                    ds.Tables[0].Columns["PD_BASICFACTOR"].Caption = "HSL hiện tại";
                    ds.Tables[0].Columns["PD_BeginDate"].Caption = "Tháng bắt đầu";
                    ds.Tables[0].Columns["PD_DateSalary"].Caption = "Tháng đến hạn";
                    ReadExcelFile.exportToExcelForEmployeeList("DANH SÁCH NHÂN VIÊN SẮP TĂNG LƯƠNG", branchName, departmentName, ds, Path.GetTempPath() + "\\" + _fileName);
                    try
                    {
                        System.Diagnostics.Process.Start(Path.GetTempPath() + "\\" + _fileName);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + _fileName, "Thông báo");
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("File " + Path.GetTempPath() + "\\" + _fileName + " đang được mở bằng một ứng dụng khác.\nHãy chọn file khác!", "Thông báo");
            }
        }

        private void btnWarning_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
          
        }

        private void btnViewEmloyee_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedCells.Count > 0)
            {
                int Employee_ID = Convert.ToInt32(dgvList.Rows[dgvList.SelectedCells[0].RowIndex].Cells["Employee_ID"].Value);
                Form f = new frmEmployeeInfo(Employee_ID,0, 0, UIParamater.Employee_Active, listRole);
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }

        private void tvBranch_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Level == 0)
            {
                if (Program.isAdmin || Program.isViewAll)
                {
                    branch_ID = 0;
                    department_ID = 0;
                }
                else
                    branch_ID = Program.intBranchID;
            }
            else if (e.Node.Level == 1)
            {
                branch_ID = Convert.ToInt32(e.Node.Tag);
                department_ID = 0;
                currPage = 0;
                LoadData();

            }
            else if (e.Node.Level >= 2)
            {
                department_ID = Convert.ToInt32(e.Node.Tag);
                branch_ID = Convert.ToInt32(e.Node.Parent.Tag);
                currPage = 0;
                LoadData();

            }
            else
                department_ID = 0;
            currPage = 0;
            LoadData();
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void frmIncreaseSalaryList_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
            if (dgvList.Columns.Count > 0)
            {
                dgvList.Columns["Employee_Code"].HeaderText = UpdateUI.GetString("maso");
                dgvList.Columns["Employee_FullName"].HeaderText = UpdateUI.GetString("hovaten");
                dgvList.Columns["Grade_Name"].HeaderText = UpdateUI.GetString("capdo");
                dgvList.Columns["PD_BasicFactor"].HeaderText = UpdateUI.GetString("hesoluongcoban");
                //dgvList.Columns["PD_BeginDate"].HeaderText = UpdateUI.GetString("ngayxetlantruoc");
                //dgvList.Columns["PD_Month"].HeaderText = UpdateUI.GetString("sothangtang");
            }
        }

        private void frmIncreaseSalaryList_FormClosing(object sender, FormClosingEventArgs e)
        {
            wcfEmployee.Close();
            wcfMain.Close();
        }

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
